package cn.saltybe.analysis.controller.api.statistic;

import cn.saltybe.analysis.entity.result.NewHouseAveragePrice;
import cn.saltybe.analysis.service.NewHouseAveragePriceStatisticServiceImpl;
import cn.saltybe.analysis.util.ApiResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/statistic/new-house-average-price")
public class NewHouseAveragePriceAPI {
    private NewHouseAveragePriceStatisticServiceImpl newHouseAveragePriceStatisticService;

    @Autowired
    public void setNewHouseAveragePriceStatisticService(NewHouseAveragePriceStatisticServiceImpl newHouseAveragePriceStatisticService) {
        this.newHouseAveragePriceStatisticService = newHouseAveragePriceStatisticService;
    }

    @ResponseBody
    @GetMapping(value = "list")
    public String list(
            @RequestParam(value = "city", required = false, defaultValue = "") String city
    ) throws Exception {
        List<NewHouseAveragePrice> list;
        if (city != null && city.length() > 0) list = newHouseAveragePriceStatisticService.query(city);
        else list = newHouseAveragePriceStatisticService.all();
        JsonArray result = new Gson().toJsonTree(list).getAsJsonArray();
        return ApiResponse.buildSuccessResponse(result).toString();
    }
}
